//
//  JrdLearnCenterViewController.m
//  v2ex-iOS
//
//  Created by longcheng on 16/6/15.
//  Copyright © 2016年 Singro. All rights reserved.
//

#import "JrdLearnCenterViewController.h"
#import "JrdCommon.h"
#import "JrdLearnInfoCalModel.h"
#import "JrdLearnCenterCell.h"
#import "LCCSqliteManager.h"
#import "JrdDatamanager.h"
@interface JrdLearnCenterViewController ()<UITableViewDataSource, UITableViewDelegate>{
    float study_time;
    int finish_course;
    int avg_score;
    LCCSqliteManager* manager;
}
@property (nonatomic, strong) SCBarButtonItem *leftBarItem;
@property (nonatomic, strong) UITableView* tableView;

//fake data
@property (nonatomic, strong) JrdLearnInfoCalModel* learnInfo;

@end



@implementation JrdLearnCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.sc_navigationItem.leftBarButtonItem = self.leftBarItem;
    [self initFakeData];
    self.view.backgroundColor  = JrdBackgroundColor;
    study_time = 0;
    finish_course = 0;
    avg_score = 0;
    //manager = [LCCSqliteManager shareInstance];
    manager = [LCCSqliteManager shareInstance];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)loadView{
    [super loadView];
    [self configureTableView];
    [self configureNav];
}

- (void)initData{
    int v_study_time = 0;
    int v_finish_time= 0;
    NSArray* node_array = [manager getSheetDataWithSheet:@"nodelist"];//[manager getSheetDataWithSheet:@"node_list"];
    for(int i = 0; i < node_array.count; i++){
        v_study_time = [node_array[i][NODE_STUDY_TIME] intValue];
        v_finish_time = [node_array[i][NODE_TOTAL_TIME] intValue];
        if((v_finish_time - v_study_time) <=2 && v_finish_time != 0){
            finish_course++;
        }
        study_time += v_study_time;
    }
    
    NSString * searchCondition = [NSString stringWithFormat:@"level_id=0 and user_id=%@",[JrdDatamanager manager].user.userId];
    NSArray* array = [manager searchDataFromSheet:@"nodelist" where:searchCondition];
    int v_total_value = 0;
    int course_num = 0;
    for(int i = 0; i < array.count; i++){
        NSString* node_id = array[0][NODE_ID];
        NSString * searchCondition = [NSString stringWithFormat:@"question_node_id=%@ and user_id=%@",
                                      node_id,[JrdDatamanager manager].user.userId];
        int total_value = 0;
        
        NSArray* nodeListData= [manager searchDataFromSheet:@"question_list" where:searchCondition];
        for(int j = 0; j<nodeListData.count; j++){
            int node_score = [nodeListData[j][3] intValue];
            total_value += node_score;
        }
        if(0 != total_value){
            course_num++;
            v_total_value+=total_value;
        }
    }
    if(0!= course_num){
        avg_score = v_total_value/course_num;
    }else{
        avg_score = 0;
    }

}
- (void)configureNav{
    self.leftBarItem = [[SCBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ico_back"]  highlight:[UIImage imageNamed:@"btn-back-normal"] style:SCBarButtonItemStylePlain handler:^(id sender) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    self.sc_navigationItem.title = NSLocalizedString(@"v2_learn_statistics",nil);//@"我们学习";
}

- (void)configureTableView {
    self.tableView                 = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - (NAVIGATION_BAR_HEIGHT))];
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate        = self;
    self.tableView.dataSource      = self;
    self.tableView.scrollEnabled  = NO;
    self.tableView.backgroundColor = JrdBackgroundColor;//JRD_RGB(240, 240, 240);
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return JRD_TABLE_LEARN_STA_CELL_HEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    JrdLearnCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[JrdLearnCenterCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    
    if(0 == indexPath.row){
        UIView *headerLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
        headerLineView.backgroundColor = JrdSpeLineColor;
        [cell addSubview:headerLineView];
        
    }
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, (JRD_TABLE_LEARN_STA_CELL_HEIGHT - 0.5), SCREEN_WIDTH, 0.5)];
    lineView.backgroundColor = JrdSpeLineColor;
    [cell addSubview:lineView];
    //[cell setIndex:indexPath.row];
    switch (indexPath.row) {
        case L_MIN:
        {
            cell.titleLabel.text = NSLocalizedString(@"jrd_learn_mins", nil);
            cell.icon.image = [UIImage imageNamed:@"bg_time"];
            cell.valueLabel.text = [NSString stringWithFormat:@"%.2f", /*(long)self.learnInfo.learn_mins*/study_time/60];
            cell.unitLabel.text = @"Mins";
            cell.valueLabel.textColor = JrdLearnMinColor;
        }
            break;
        case L_COURSE:{
            cell.titleLabel.text = NSLocalizedString(@"jrd_learn_course", nil);
            cell.icon.image = [UIImage imageNamed:@"bg_course"];
            cell.valueLabel.text = [NSString stringWithFormat:@"%d", finish_course/*(long)self.learnInfo.finish_course*/];
            cell.unitLabel.text = @"Course";
            cell.valueLabel.textColor = JrdFinishCourseColor;
        }
            break;
        case L_R_SCORE:{
            cell.titleLabel.text = NSLocalizedString(@"jrd_require_score", nil);
            cell.icon.image = [UIImage imageNamed:@"bg_score"];
            cell.valueLabel.text = [NSString stringWithFormat:@"%d", /*(long)self.learnInfo.score_require*/
                                    avg_score];
            cell.unitLabel.text = @"Score";
            cell.valueLabel.textColor = JrdScoreReuColor;
        }
            break;
        /*case L_E_SCORE:{
            cell.titleLabel.text = NSLocalizedString(@"jrd_eletive_score", nil);
            cell.icon.image = [UIImage imageNamed:@"bg_electivecourse"];
            cell.valueLabel.text = [NSString stringWithFormat:@"%ld", (long)self.learnInfo.score_elective];
            cell.unitLabel.text = @"Score";
            cell.valueLabel.textColor = JrdScoreDelColor;
        }
            break;*/
        default:
            break;
    }
    [cell updateUnitText];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)initFakeData{
    self.learnInfo = [[JrdLearnInfoCalModel alloc] init];
    self.learnInfo.learn_mins = 60;
    self.learnInfo.finish_course = 2;
    self.learnInfo.score_require = 59;
    self.learnInfo.score_elective = 69;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.size.width, 10)];
    headerView.backgroundColor = JrdTableHeadColor;//[UIColor clearColor];
    return headerView;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height
{
    UIView* footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.size.width, 5)];
    footView.backgroundColor = [UIColor clearColor];
    return footView;
}
@end
